<html>
<head><meta charset="utf-8"><title>Thread panics/assertions and program termination · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Thread.20panics.2Fassertions.20and.20program.20termination.html">Thread panics/assertions and program termination</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="186825055"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Thread%20panics/assertions%20and%20program%20termination/near/186825055" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Thread.20panics.2Fassertions.20and.20program.20termination.html#186825055">(Jan 28 2020 at 20:28)</a>:</h4>
<p>In a test case, I need to spawn a thread (<code>std::thread::spawn</code>). How can I safely do assertions in that thread, as part of the test case? As far as I can tell, the thread panics but the test keeps running. If the thread fails, the main thread will run forever, so the test case keeps running forever.</p>



<a name="186825561"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Thread%20panics/assertions%20and%20program%20termination/near/186825561" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Laurențiu <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Thread.20panics.2Fassertions.20and.20program.20termination.html#186825561">(Jan 28 2020 at 20:33)</a>:</h4>
<p>Can you <code>join</code> it?</p>



<a name="186825687"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Thread%20panics/assertions%20and%20program%20termination/near/186825687" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Thread.20panics.2Fassertions.20and.20program.20termination.html#186825687">(Jan 28 2020 at 20:34)</a>:</h4>
<p>The main thread is waiting on something else, and waiting on both the something else and the child thread would substantially complicate the logic.</p>



<a name="186825786"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Thread%20panics/assertions%20and%20program%20termination/near/186825786" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Laurențiu <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Thread.20panics.2Fassertions.20and.20program.20termination.html#186825786">(Jan 28 2020 at 20:35)</a>:</h4>
<p>I suppose you'll need to synchronize them some way, depending on what the main thread is doing.</p>



<a name="186825789"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Thread%20panics/assertions%20and%20program%20termination/near/186825789" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Thread.20panics.2Fassertions.20and.20program.20termination.html#186825789">(Jan 28 2020 at 20:35)</a>:</h4>
<p>(The child thread exists to call <code>wait</code> on a child process, and then notify the main thread via something it's already watching.)</p>



<a name="186825950"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Thread%20panics/assertions%20and%20program%20termination/near/186825950" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Thread.20panics.2Fassertions.20and.20program.20termination.html#186825950">(Jan 28 2020 at 20:36)</a>:</h4>
<p>(I really wish that <code>pidfd</code> worked everywhere, but even <em>with</em> that it'd be substantially more complex than the simple test case I'm trying to write.)</p>



<a name="186826135"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Thread%20panics/assertions%20and%20program%20termination/near/186826135" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Laurențiu <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Thread.20panics.2Fassertions.20and.20program.20termination.html#186826135">(Jan 28 2020 at 20:38)</a>:</h4>
<p>Do you expect the helper thread to panic (outside of the case when the child could not be launched)?</p>



<a name="186826240"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Thread%20panics/assertions%20and%20program%20termination/near/186826240" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Thread.20panics.2Fassertions.20and.20program.20termination.html#186826240">(Jan 28 2020 at 20:39)</a>:</h4>
<p>Even that case would be problematic, as I'd like to not have the test case turn into an infinite loop in that case. But yes, the helper thread could also <code>assert!</code>.</p>



<a name="186826249"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Thread%20panics/assertions%20and%20program%20termination/near/186826249" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Laurențiu <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Thread.20panics.2Fassertions.20and.20program.20termination.html#186826249">(Jan 28 2020 at 20:39)</a>:</h4>
<p>Like.. if you do <code>Command::new("foo").status()</code>, it's probably not going to panic, even if I don't think it's really documented not to.</p>



<a name="186826340"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Thread%20panics/assertions%20and%20program%20termination/near/186826340" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Thread.20panics.2Fassertions.20and.20program.20termination.html#186826340">(Jan 28 2020 at 20:40)</a>:</h4>
<p>I actually call <code>.spawn()</code> in the main thread, and then pass the resulting <code>Child</code> to the helper thread.</p>



<a name="186826356"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Thread%20panics/assertions%20and%20program%20termination/near/186826356" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Thread.20panics.2Fassertions.20and.20program.20termination.html#186826356">(Jan 28 2020 at 20:40)</a>:</h4>
<p>So that case will panic in the main thread.</p>



<a name="186826404"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Thread%20panics/assertions%20and%20program%20termination/near/186826404" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Thread.20panics.2Fassertions.20and.20program.20termination.html#186826404">(Jan 28 2020 at 20:40)</a>:</h4>
<p>The helper thread just calls <code>let status = child.wait().expect("...");</code> and <code>assert!(status.success());</code>.</p>



<a name="186826434"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Thread%20panics/assertions%20and%20program%20termination/near/186826434" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Laurențiu <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Thread.20panics.2Fassertions.20and.20program.20termination.html#186826434">(Jan 28 2020 at 20:41)</a>:</h4>
<p>Why not send <code>status</code> over a channel to the main thread?</p>



<a name="186826528"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Thread%20panics/assertions%20and%20program%20termination/near/186826528" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Thread.20panics.2Fassertions.20and.20program.20termination.html#186826528">(Jan 28 2020 at 20:42)</a>:</h4>
<p>I very easily could (the helper thread exists to wait and then pass data to the main thread, and this is a test case <em>for</em> a channel-like construct). But it feels fragile that if the child thread panics for any reason the parent thread will hang.</p>



<a name="186826552"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Thread%20panics/assertions%20and%20program%20termination/near/186826552" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Thread.20panics.2Fassertions.20and.20program.20termination.html#186826552">(Jan 28 2020 at 20:42)</a>:</h4>
<p>I'd like something better than "carefully avoid panicking".</p>



<a name="186826605"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Thread%20panics/assertions%20and%20program%20termination/near/186826605" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Laurențiu <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Thread.20panics.2Fassertions.20and.20program.20termination.html#186826605">(Jan 28 2020 at 20:43)</a>:</h4>
<p>There is <code>catch_unwind</code>, I suppose, so you could wrap your helper thread logic inside that</p>



<a name="186826781"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Thread%20panics/assertions%20and%20program%20termination/near/186826781" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Laurențiu <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Thread.20panics.2Fassertions.20and.20program.20termination.html#186826781">(Jan 28 2020 at 20:44)</a>:</h4>
<p>I don't have other ideas, sorry</p>



<a name="186826801"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Thread%20panics/assertions%20and%20program%20termination/near/186826801" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Thread.20panics.2Fassertions.20and.20program.20termination.html#186826801">(Jan 28 2020 at 20:44)</a>:</h4>
<p>Yeah, I could certainly go there. But in the case of a test case, what <em>can</em> I call from the thread that'll cause the test to fail, and ideally still run destructors on the way out?</p>



<a name="186827023"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Thread%20panics/assertions%20and%20program%20termination/near/186827023" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Laurențiu <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Thread.20panics.2Fassertions.20and.20program.20termination.html#186827023">(Jan 28 2020 at 20:46)</a>:</h4>
<p>That depends on what the main thread (test) is doing. If they use a channel to communicate, it can detect that the sender was closed. But there's no "test framework runtime" per se that could detect that a background thread has panicked.</p>



<a name="186827094"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Thread%20panics/assertions%20and%20program%20termination/near/186827094" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Thread.20panics.2Fassertions.20and.20program.20termination.html#186827094">(Jan 28 2020 at 20:47)</a>:</h4>
<p>Unfortunately, the nature of the channel I'm using is that you can't tell if the other end was closed.</p>



<a name="186827118"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Thread%20panics/assertions%20and%20program%20termination/near/186827118" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Thread.20panics.2Fassertions.20and.20program.20termination.html#186827118">(Jan 28 2020 at 20:47)</a>:</h4>
<p>Which is why the main thread would block forever: it's waiting on a message that'll never come.</p>



<a name="186827231"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Thread%20panics/assertions%20and%20program%20termination/near/186827231" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Laurențiu <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Thread.20panics.2Fassertions.20and.20program.20termination.html#186827231">(Jan 28 2020 at 20:48)</a>:</h4>
<p>And I guess there's a reason why you can't use a standard channel to pass back the result, regardless of what the test is.. testing.</p>



<a name="186827295"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Thread%20panics/assertions%20and%20program%20termination/near/186827295" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Thread.20panics.2Fassertions.20and.20program.20termination.html#186827295">(Jan 28 2020 at 20:49)</a>:</h4>
<p>Right. Because the main thread can't simultaneously wait on a standard channel and the channel I'm testing, without a lot more complexity in the test.</p>



<a name="186827432"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Thread%20panics/assertions%20and%20program%20termination/near/186827432" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Laurențiu <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Thread.20panics.2Fassertions.20and.20program.20termination.html#186827432">(Jan 28 2020 at 20:50)</a>:</h4>
<p>Then you could spawn a third thread to wait on the channel you're testing and have the main wait for both (using channels, if you need to)</p>



<a name="186827475"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Thread%20panics/assertions%20and%20program%20termination/near/186827475" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Laurențiu <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Thread.20panics.2Fassertions.20and.20program.20termination.html#186827475">(Jan 28 2020 at 20:51)</a>:</h4>
<p>Yeah, that's a bit awkward</p>



<a name="186827479"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Thread%20panics/assertions%20and%20program%20termination/near/186827479" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Thread.20panics.2Fassertions.20and.20program.20termination.html#186827479">(Jan 28 2020 at 20:51)</a>:</h4>
<p>Heh. True, and true. :)</p>



<a name="186827509"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Thread%20panics/assertions%20and%20program%20termination/near/186827509" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Thread.20panics.2Fassertions.20and.20program.20termination.html#186827509">(Jan 28 2020 at 20:52)</a>:</h4>
<p>I think I'm more broadly trying to figure out how people write tests involving threads in a less painful way.</p>



<a name="186827757"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Thread%20panics/assertions%20and%20program%20termination/near/186827757" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Laurențiu <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Thread.20panics.2Fassertions.20and.20program.20termination.html#186827757">(Jan 28 2020 at 20:54)</a>:</h4>
<p>With a lot of joining and channels, I'd wager :-). One thing I like about the Windows API is that you can wait on basically every type of handle.</p>



<a name="186827831"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Thread%20panics/assertions%20and%20program%20termination/near/186827831" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Thread.20panics.2Fassertions.20and.20program.20termination.html#186827831">(Jan 28 2020 at 20:55)</a>:</h4>
<p>The same is true on Linux with fds and select/poll/epoll, as long as you have an fd. signalfd and pidfd make it possible to wait on more things as fds.</p>



<a name="186827887"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Thread%20panics/assertions%20and%20program%20termination/near/186827887" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Laurențiu <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Thread.20panics.2Fassertions.20and.20program.20termination.html#186827887">(Jan 28 2020 at 20:55)</a>:</h4>
<p>And timerfd :D</p>



<a name="186827950"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Thread%20panics/assertions%20and%20program%20termination/near/186827950" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Thread.20panics.2Fassertions.20and.20program.20termination.html#186827950">(Jan 28 2020 at 20:56)</a>:</h4>
<p>And timerfd, yes. :)</p>



<a name="186828009"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Thread%20panics/assertions%20and%20program%20termination/near/186828009" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Laurențiu <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Thread.20panics.2Fassertions.20and.20program.20termination.html#186828009">(Jan 28 2020 at 20:57)</a>:</h4>
<p>Hmm, can you pass the thread a helper struct that wraps a channel <code>Sender</code> and checks <code>thread::panicking</code> in <code>drop</code>?</p>



<a name="186828070"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Thread%20panics/assertions%20and%20program%20termination/near/186828070" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Thread.20panics.2Fassertions.20and.20program.20termination.html#186828070">(Jan 28 2020 at 20:57)</a>:</h4>
<p>...devious. Thinking...</p>



<a name="186828235"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Thread%20panics/assertions%20and%20program%20termination/near/186828235" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Laurențiu <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Thread.20panics.2Fassertions.20and.20program.20termination.html#186828235">(Jan 28 2020 at 20:59)</a>:</h4>
<p>Actually, the real problem might be how to wait for one thread out of a set</p>



<a name="186828237"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Thread%20panics/assertions%20and%20program%20termination/near/186828237" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Thread.20panics.2Fassertions.20and.20program.20termination.html#186828237">(Jan 28 2020 at 20:59)</a>:</h4>
<blockquote>
<p>This can also be used in multithreaded applications, in order to send a message to other threads warning that a thread has panicked (e.g., for monitoring purposes).</p>
</blockquote>



<a name="186828243"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Thread%20panics/assertions%20and%20program%20termination/near/186828243" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Thread.20panics.2Fassertions.20and.20program.20termination.html#186828243">(Jan 28 2020 at 20:59)</a>:</h4>
<p>Yeah, that's pretty much what I need...</p>



<a name="186828247"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Thread%20panics/assertions%20and%20program%20termination/near/186828247" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Thread.20panics.2Fassertions.20and.20program.20termination.html#186828247">(Jan 28 2020 at 20:59)</a>:</h4>
<p>Wouldn't necessarily even have to use a channel, could just print a message to stderr and abort. That sounds like a useful primitive for thread testing.</p>



<a name="186828360"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Thread%20panics/assertions%20and%20program%20termination/near/186828360" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Laurențiu <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Thread.20panics.2Fassertions.20and.20program.20termination.html#186828360">(Jan 28 2020 at 21:00)</a>:</h4>
<blockquote>
<p>Actually, the real problem might be how to wait for one thread out of a set</p>
</blockquote>
<p>Because if one of them panics you know you can kill the rest</p>



<a name="186828412"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Thread%20panics/assertions%20and%20program%20termination/near/186828412" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Laurențiu <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Thread.20panics.2Fassertions.20and.20program.20termination.html#186828412">(Jan 28 2020 at 21:01)</a>:</h4>
<p>If you abort, the other tests won't run, right?</p>



<a name="186828545"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Thread%20panics/assertions%20and%20program%20termination/near/186828545" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Thread.20panics.2Fassertions.20and.20program.20termination.html#186828545">(Jan 28 2020 at 21:02)</a>:</h4>
<p>Good point.</p>



<a name="186828552"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Thread%20panics/assertions%20and%20program%20termination/near/186828552" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Thread.20panics.2Fassertions.20and.20program.20termination.html#186828552">(Jan 28 2020 at 21:02)</a>:</h4>
<p>It would be nice to be able to abort the <em>test</em> without aborting the entire <em>test framework</em>.</p>



<a name="186828617"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Thread%20panics/assertions%20and%20program%20termination/near/186828617" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Laurențiu <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Thread.20panics.2Fassertions.20and.20program.20termination.html#186828617">(Jan 28 2020 at 21:03)</a>:</h4>
<p>So how do you wait for one thread out of many? I can't find a way.</p>



<a name="186828709"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Thread%20panics/assertions%20and%20program%20termination/near/186828709" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Thread.20panics.2Fassertions.20and.20program.20termination.html#186828709">(Jan 28 2020 at 21:04)</a>:</h4>
<p>I'm not sure I know what you mean? There are several ways to wait for a thread, but how does that solve the problem of "kill the test (with a failure)"?</p>



<a name="186828940"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Thread%20panics/assertions%20and%20program%20termination/near/186828940" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Laurențiu <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Thread.20panics.2Fassertions.20and.20program.20termination.html#186828940">(Jan 28 2020 at 21:05)</a>:</h4>
<p>As in "wait for one", not "wait for all" of them. My reasoning is that you can spawn your two helper threads, wait for either of them to finish, and if it panicked, kill the other thread and propagate the panic.</p>



<a name="186829021"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Thread%20panics/assertions%20and%20program%20termination/near/186829021" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Thread.20panics.2Fassertions.20and.20program.20termination.html#186829021">(Jan 28 2020 at 21:06)</a>:</h4>
<p>Ah, I see what you're getting at.</p>



<a name="186829055"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Thread%20panics/assertions%20and%20program%20termination/near/186829055" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Laurențiu <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Thread.20panics.2Fassertions.20and.20program.20termination.html#186829055">(Jan 28 2020 at 21:06)</a>:</h4>
<p>The main issue seems to be that you can't cancel the read from the channel thingy you're testing</p>



<a name="186829086"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Thread%20panics/assertions%20and%20program%20termination/near/186829086" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Thread.20panics.2Fassertions.20and.20program.20termination.html#186829086">(Jan 28 2020 at 21:07)</a>:</h4>
<p>If I decided to go with the "two helper threads" approach, I could just wait on the helper thread then the main thread, and panic if either panics.</p>



<a name="186829093"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Thread%20panics/assertions%20and%20program%20termination/near/186829093" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Thread.20panics.2Fassertions.20and.20program.20termination.html#186829093">(Jan 28 2020 at 21:07)</a>:</h4>
<p>But that's still awkward.</p>



<a name="186829119"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Thread%20panics/assertions%20and%20program%20termination/near/186829119" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Thread.20panics.2Fassertions.20and.20program.20termination.html#186829119">(Jan 28 2020 at 21:07)</a>:</h4>
<p><span class="user-mention" data-user-id="203546">@Laurențiu Nicola</span> The thing I'm testing is roughly equivalent to a UDP socket.</p>



<a name="186829163"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Thread%20panics/assertions%20and%20program%20termination/near/186829163" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Thread.20panics.2Fassertions.20and.20program.20termination.html#186829163">(Jan 28 2020 at 21:08)</a>:</h4>
<p>You don't get a definitive EOF-like signal, because there's no connection.</p>



<a name="186829237"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Thread%20panics/assertions%20and%20program%20termination/near/186829237" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Laurențiu <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Thread.20panics.2Fassertions.20and.20program.20termination.html#186829237">(Jan 28 2020 at 21:08)</a>:</h4>
<blockquote>
<p>If I decided to go with the "two helper threads" approach, I could just wait on the helper thread then the main thread, and panic if either panics.</p>
</blockquote>
<p>But if there's no way to wait for one of them, you'll need to use channels (probably from <code>crossbeam</code>, since they have <code>select</code>).</p>



<a name="186829254"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Thread%20panics/assertions%20and%20program%20termination/near/186829254" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Thread.20panics.2Fassertions.20and.20program.20termination.html#186829254">(Jan 28 2020 at 21:08)</a>:</h4>
<p>"wait on the helper thread then the main thread" could just use join.</p>



<a name="186829288"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Thread%20panics/assertions%20and%20program%20termination/near/186829288" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Laurențiu <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Thread.20panics.2Fassertions.20and.20program.20termination.html#186829288">(Jan 28 2020 at 21:09)</a>:</h4>
<p>If it's a single one, yes, but in the general case you might want to wait for either of them to finish</p>



<a name="186829490"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Thread%20panics/assertions%20and%20program%20termination/near/186829490" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Laurențiu <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Thread.20panics.2Fassertions.20and.20program.20termination.html#186829490">(Jan 28 2020 at 21:10)</a>:</h4>
<blockquote>
<p>You don't get a definitive EOF-like signal, because there's no connection.</p>
</blockquote>
<p>Got it. But if it was a socket, you could close it, which would result in a read error, so your thread would know it needs to shut down</p>



<a name="186829568"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Thread%20panics/assertions%20and%20program%20termination/near/186829568" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Thread.20panics.2Fassertions.20and.20program.20termination.html#186829568">(Jan 28 2020 at 21:12)</a>:</h4>
<blockquote>
<p>If it's a single one, yes, but in the general case you might want to wait for either of them to finish</p>
</blockquote>
<p>Sure. And if I can get Rust to add better support for threaded test cases, it might require something more sophisticated. :)</p>



<a name="186829653"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Thread%20panics/assertions%20and%20program%20termination/near/186829653" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Thread.20panics.2Fassertions.20and.20program.20termination.html#186829653">(Jan 28 2020 at 21:12)</a>:</h4>
<blockquote>
<blockquote>
<p>You don't get a definitive EOF-like signal, because there's no connection.</p>
</blockquote>
<p>Got it. But if it was a socket, you could close it, which would result in a read error, so your thread would know it needs to shut down</p>
</blockquote>
<p>Closing a sender to a UDP socket (or equivalent) doesn't result in any indication to the receiving socket.</p>



<a name="186829711"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Thread%20panics/assertions%20and%20program%20termination/near/186829711" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Thread.20panics.2Fassertions.20and.20program.20termination.html#186829711">(Jan 28 2020 at 21:13)</a>:</h4>
<p>(I would <em>love</em> a suggestion otherwise. The whole "helper thread" mechanism here exists to work around the lack of any EOF mechanism.)</p>



<a name="186829769"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Thread%20panics/assertions%20and%20program%20termination/near/186829769" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Thread.20panics.2Fassertions.20and.20program.20termination.html#186829769">(Jan 28 2020 at 21:14)</a>:</h4>
<p>I'm working with <code>AF_UNIX</code> <code>SOCK_DGRAM</code> sockets.</p>



<a name="186829778"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Thread%20panics/assertions%20and%20program%20termination/near/186829778" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Thread.20panics.2Fassertions.20and.20program.20termination.html#186829778">(Jan 28 2020 at 21:14)</a>:</h4>
<p><code>std::os::unix::net::UnixDatagram</code></p>



<a name="186830169"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Thread%20panics/assertions%20and%20program%20termination/near/186830169" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Laurențiu <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Thread.20panics.2Fassertions.20and.20program.20termination.html#186830169">(Jan 28 2020 at 21:19)</a>:</h4>
<p>I meant closing the receiving end. I might have drifted too much from your original question, but I was thinking that you:</p>
<ol start="0">
<li>are writing a test for reading from a socket</li>
<li>can spawn a thread that tries to read and blocks</li>
<li>spawn another thread that uses an external process to send data to your socket</li>
<li>want to detect if either of the two panics, so you can fail the test, and preferably clean up</li>
<li>with a "wait one thread" primitive you can wait for one of the two to finish, decide if the test failed or not, then signal the other thread to close (or kill it)</li>
<li>you can signal the socket thread to shut down by closing the socket it's reading from</li>
</ol>



<a name="186830349"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Thread%20panics/assertions%20and%20program%20termination/near/186830349" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Laurențiu <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Thread.20panics.2Fassertions.20and.20program.20termination.html#186830349">(Jan 28 2020 at 21:21)</a>:</h4>
<p>Anyway, you already know these things ^</p>



<a name="186830468"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Thread%20panics/assertions%20and%20program%20termination/near/186830468" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Thread.20panics.2Fassertions.20and.20program.20termination.html#186830468">(Jan 28 2020 at 21:22)</a>:</h4>
<p>Closing the socket that thread is reading from is a potentially hazardous way of making a thread error, since a more complex test could potentially open another file that ends up with the same descriptor.</p>



<a name="186830613"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Thread%20panics/assertions%20and%20program%20termination/near/186830613" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Laurențiu <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Thread.20panics.2Fassertions.20and.20program.20termination.html#186830613">(Jan 28 2020 at 21:24)</a>:</h4>
<p>Yeah, that's a good point. I don't think there's a race in the simple case, but it could turn into a mess</p>



<a name="186830729"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Thread%20panics/assertions%20and%20program%20termination/near/186830729" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Thread.20panics.2Fassertions.20and.20program.20termination.html#186830729">(Jan 28 2020 at 21:25)</a>:</h4>
<p>I'm also realizing that Rust doesn't seem to expose "kill this thread".</p>



<a name="186830823"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Thread%20panics/assertions%20and%20program%20termination/near/186830823" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Thread.20panics.2Fassertions.20and.20program.20termination.html#186830823">(Jan 28 2020 at 21:26)</a>:</h4>
<p>Given a <code>JoinHandle</code>, it should be possible to terminate a thread.</p>



<a name="186830888"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Thread%20panics/assertions%20and%20program%20termination/near/186830888" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Laurențiu <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Thread.20panics.2Fassertions.20and.20program.20termination.html#186830888">(Jan 28 2020 at 21:27)</a>:</h4>
<p>I thought there was a deprecated method for that, but it might have been removed</p>



<a name="194627032"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Thread%20panics/assertions%20and%20program%20termination/near/194627032" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Peter Rabbit <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Thread.20panics.2Fassertions.20and.20program.20termination.html#194627032">(Apr 20 2020 at 01:43)</a>:</h4>
<p>Terminating a thread is not a particularly safe operation. If it was in the process of writing to any shared data, that data can be left in a corrupt state and cause UB when other threads access it. Also if you use scoped threads, all the stack memory is deallocated so the scoped thread now might accidentally access invalid memory.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>